﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using COMOSOFT.Main;
using DevExpress.XtraGrid.Views.Grid;

namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmMAGeneralView : Form
    {
        //Task tak;
        Image img;
        int year;
        object sup;
        FrmMALibrary FrmMALib = null;
        FrmMain frmain = null;
        DataTable dbTemp = null;
        public FrmMAGeneralView(FrmMain _frmain)
        {
            InitializeComponent();
            this.frmain = _frmain;
        }
        public void LoadMAGV(int _year, object _sup)
        {
            this.year = _year;
            this.sup = _sup;
            CallMALibrary(year, sup);            
            radioGroup_MAList.SelectedIndex = 0;
            radioGroup_MAList.Properties.Items[0].Description = "All MA " + this.year.ToString();
            LoadData(year, sup);
        }
        private void CallMALibrary(int year,object sup)
        {
        //    try
        //    {               
        //        if (tak == null || tak.Status != TaskStatus.Running)
        //        {
        //            progressPanel1.Show();
        //            progressPanel1.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - progressPanel1.Width, Screen.PrimaryScreen.WorkingArea.Height / 2 - progressPanel1.Height);
        //            gridControl_MAGaneral.DataSource = null;
        //            tak = Task.Factory.StartNew(() => LoadData(year,sup));
        //            tak.ContinueWith(tt => FillTable());
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        //}
        //private delegate void dlgFillTable();
        //private void FillTable()
        //{
        //    try
        //    {
        //        if (InvokeRequired)
        //        {
        //            this.Invoke(new dlgFillTable(FillTable));
        //        }
        //        else
        //        {
        //            gridControl_MAGaneral.DataSource = dataSetMatarial.sp_MAGeneral;
                    progressPanel1.Hide();
        //        }
        //    }
        //    catch
        //    {
        //        MessageBox.Show("Progess fail");
        //    }
        }

        #region [Load Data]
        private void LoadData(int year, object sup)
        {
            try
            {
                sp_MAGeneralTableAdapter.Fill(this.dataSetMatarial.sp_MAGeneral, year, 0, sup);
                dbTemp = new DataTable();          
                dbTemp.Columns.Add("ID");
                dbTemp.Columns.Add("Img", typeof(object));
                DataTable dbMA = dataSetMatarial.sp_MAGeneral;
                for (int i = 0; i < dbMA.Rows.Count; i++)
                {
                    DataRow newrow = dbTemp.NewRow();
                    string path = dbMA.Rows[i]["Picture"].ToString();
                    if (path != null && path != "")
                    {
                        byte[] img = System.IO.File.ReadAllBytes(path);
                        newrow["ID"] = dbMA.Rows[i]["MADID"];
                        newrow["Img"] = img;
                        dbTemp.Rows.Add(newrow);
                    }
                }               
            }
            catch
            {
                MessageBox.Show("Disconnection database! You can try later");
            }
        }
        #endregion

        #region [Select Radio]
        private void radioGroup_MAList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (radioGroup_MAList.SelectedIndex == 0)
                {
                    sp_MAGeneralTableAdapter.Fill(this.dataSetMatarial.sp_MAGeneral, year, 0, null);
                }
                else
                {
                    sp_MAGeneralTableAdapter.Fill(this.dataSetMatarial.sp_MAGeneral, year, 1, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [FromLoad]
        private void FrmMAGeneralView_Load(object sender, EventArgs e)
        {            
            this.employeeTableAdapter.Fill(this.dataSetOrderManagement.Employee);
        }
        #endregion

        #region [DoubleClick Gridview]
        private void gridView_MAGeneral_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int MAID = Convert.ToInt32(gridView_MAGeneral.GetFocusedDataRow()["MAID"]);
                if (FrmMALib != null && !FrmMALib.IsDisposed)
                {
                    FrmMALib.Select();
                    FrmMALib.LoadData();
                }
                else
                {
                    FrmMALib = new FrmMALibrary(MAID);
                    FrmMALib.MdiParent = this.frmain;
                    FrmMALib.Show();
                    FrmMALib.LoadData();                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region [Show Image]
        private Image LoadImg(string ID, int row)
        {    
            if (dbTemp.Rows.Count>row && dbTemp.Rows[row]["ID"].ToString() == ID)
            {
                using (System.IO.MemoryStream str = new System.IO.MemoryStream(dbTemp.Rows[row]["Img"] as byte[]))
                {
                    img = Image.FromStream(str);
                    return img;
                }
            }
            byte[] NoImg = System.IO.File.ReadAllBytes(@"D:\\noImage.gif");
            using (System.IO.MemoryStream str = new System.IO.MemoryStream(NoImg as byte[]))
            {
                img = Image.FromStream(str);
            }
            return img;
        }
        private void gridView_MAGeneral_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;              
                if (e.Column.FieldName == "Pictures" && e.IsGetData)
                    e.Value = LoadImg(view.GetRowCellValue(e.ListSourceRowIndex, "MADID").ToString(), e.ListSourceRowIndex);         
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sp_MAGeneralTableAdapter.Fill(this.dataSetMatarial.sp_MAGeneral, year, 1, null);
        }
    }
}
